package com.deli.pagingsimple;

import android.app.Application;

import androidx.annotation.NonNull;
import androidx.lifecycle.AndroidViewModel;
import androidx.paging.PagingData;
import androidx.paging.PagingSource;

import com.alibaba.fastjson.JSONObject;
import com.deli.pagingforjava.bean.BaseItemBean;
import com.deli.pagingforjava.PagingHelper;
import com.deli.pagingforjava.SimplePagingSource;
import com.deli.pagingsimple.bean.CategoryBean;
import com.deli.pagingsimple.bean.HeaderBean;
import com.deli.pagingsimple.bean.ItemBean;
import com.deli.pagingforjava.bean.NoMoreDataBean;

import java.util.ArrayList;
import java.util.List;

import io.reactivex.Flowable;
import io.reactivex.SingleEmitter;

/**
 * @Author: QDL
 * @CreateDate: 2021/4/23 14:21
 */
public class MainViewModel extends AndroidViewModel {
    Flowable<PagingData<BaseItemBean>> pagingData;
    SimplePagingSource.PagingSourceRequest<Integer, BaseItemBean> callBack;

    int position = 1;

    public MainViewModel(@NonNull Application application) {
        super(application);
    }

    public void init() {
        pagingData = PagingHelper.getDefault()
                .getFlowable(new SimplePagingSource.PagingSourceRequest<Integer, BaseItemBean>() {
                    @Override
                    public void handleRequest(SingleEmitter<List<BaseItemBean>> emitter, PagingSource.LoadParams<Integer> params) {
                        String response = getRequestJson();
                        JSONObject jsonObject = JSONObject.parseObject(response);
                        List<BaseItemBean> list = new ArrayList<>();
                        if (params.getKey() == null || params.getKey() == 1) {
                            list.add(new HeaderBean());
                            list.add(new CategoryBean());
                        }

                        // emitter.onSuccess(list);
                        if (params.getKey() != null && params.getKey() == 2) {
                            list.addAll(JSONObject.parseArray(jsonObject.getString("data"), ItemBean.class));
                            list.add(new NoMoreDataBean());
                            emitter.onSuccess(list);
                        } else {
                            list.addAll(JSONObject.parseArray(jsonObject.getString("data"), ItemBean.class));
                            emitter.onSuccess(list);
                        }
                    }

                    @Override
                    public Integer getNextPageKey(PagingSource.LoadParams<Integer> params, List<BaseItemBean> list) {
                        if (params.getKey() != null && params.getKey() == 2) {
                            return null;
                        }
                        return params.getKey() == null ? 2 : params.getKey() + 1;
                    }
                });

    }


    /**
     * 模拟数据
     *
     * @return
     */
    public synchronized String getRequestJson() {
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("code", 0);
        jsonObject.put("msg", "success");

        List<BaseItemBean> data = new ArrayList<>();
        for (int i = 0; i < 10; i++) {
            data.add(new ItemBean("ITEM" + position));
            position++;
        }

        jsonObject.put("data", data);
        return jsonObject.toJSONString();
    }

}
